package com.service.impl;

import com.constant.Constant;
import com.dao.GoldPointsDao;
import com.entity.GoldPoints;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.GoldPointsService;
import com.util.AjaxResult;
import com.vo.GoldPointsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class GoldPointsServiceImpl implements GoldPointsService {
    @Autowired
   private GoldPointsDao goldPointsDao;

    @Override
    public int updatePAG(int Uid, Integer points) {
        Integer id = Integer.valueOf(Uid);
        GoldPoints goldPoints = new GoldPoints(id,0,points/10,"积分换金币",new Date());
        GoldPoints goldPoints1 = new GoldPoints(id,-points,0,"积分换金币",new Date());
        int i = goldPointsDao.insertGoldById(goldPoints1);
        int j=goldPointsDao.insertGoldById(goldPoints);
        return i+j;
    }

    //积分兑换金币
    @Override
    public AjaxResult exchangeDold(int points, int id) {
        if (points%10!=0){
            return AjaxResult.isFail("请输入10的倍数的积分数！");
        }
        int gold=points/10;
        GoldPoints goldPoints=new GoldPoints(id,-points,gold,points+"积分兑换了"+gold+"金币",new Date());
        goldPointsDao.insertGoldPoints(goldPoints);
        return AjaxResult.isOk("您成功兑换了"+gold+"个金币！");
    }
    //查询积分金币
    @Override
    public PageInfo findGoldPointByPageInNo(GoldPoints goldPoints, Integer pageNo) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<GoldPoints> goldPointsList= goldPointsDao.selectGoldPointPageNo(goldPoints);
        PageInfo<GoldPoints> info=new PageInfo<>(goldPointsList);
        System.out.println(goldPointsList);
        return info;
    }

    @Override
    public GoldPointsVo findGoldPointsVoByUid(int uid) {
        GoldPointsVo goldPointsVo = goldPointsDao.getGoldPointsVo(uid);
        System.out.println(goldPointsVo);
        return goldPointsVo;
    }

    @Override
    public PageInfo<GoldPoints> findUserRecord(User user, Integer pageNo) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<GoldPoints> list = goldPointsDao.selectRecord(user);
        if (null == list) {
            throw new ServiceException("没有记录...");
        }
        return new PageInfo<GoldPoints>(list);
    }


}
